#include "numpad.h"

#include <QPushButton>
#include <QHBoxLayout>
#include <QKeyEvent>

#include "settings.h"

Numpad::Numpad(QWidget *parent) :
    QWidget(parent)
{
    signalMapper = new QSignalMapper(this);
    QGridLayout *layout = new QGridLayout(this);
    layout->setSpacing(5);

    QPushButton *bt;

    QString sName;

    QSettings *settings = Settings::getInstance();

    setMaximumSize(settings->value("globals/button_width").toInt() * 4 + settings->value("globals/button_hmargin").toInt() * 4, settings->value("globals/button_height").toInt() * 5 + settings->value("globals/button_vmargin").toInt() * 5);
    setMinimumSize(settings->value("globals/button_width").toInt() * 4 + settings->value("globals/button_hmargin").toInt() * 4, settings->value("globals/button_height").toInt() * 5 + settings->value("globals/button_vmargin").toInt() * 5);

    for (int i=0; i<3; ++i){
        for (int j=0; j<3; j++){
            sName = QString::number(i*3+j+1);
            bt = new QPushButton(sName, this);
            bt->setMaximumSize(settings->value("globals/button_width").toInt(), settings->value("globals/button_height").toInt());
            bt->setFocusPolicy(Qt::NoFocus);
            connect(bt, SIGNAL(clicked()), signalMapper, SLOT(map()));
            signalMapper->setMapping(bt, sName);
            layout->addWidget(bt,1+(2-i),j);
        }
    }

    sName = "0";
    bt = new QPushButton(sName, this);
    bt->setMaximumSize(settings->value("globals/button_width").toInt(), settings->value("globals/button_height").toInt());
    bt->setFocusPolicy(Qt::NoFocus);
    connect(bt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(bt, sName);
    layout->addWidget(bt,4,0);

    sName = "00";
    bt = new QPushButton(sName, this);
    bt->setMaximumSize(settings->value("globals/button_width").toInt(), settings->value("globals/button_height").toInt());
    bt->setFocusPolicy(Qt::NoFocus);
    connect(bt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(bt, sName);
    layout->addWidget(bt,4,1);

    sName = ".";
    bt = new QPushButton(sName, this);
    bt->setMaximumSize(settings->value("globals/button_width").toInt(), settings->value("globals/button_height").toInt());
    bt->setFocusPolicy(Qt::NoFocus);
    connect(bt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(bt, sName);
    layout->addWidget(bt,4,2);

    sName = "Ok";
    bt = new QPushButton(sName, this);
    bt->setMaximumSize(settings->value("globals/button_width").toInt(), settings->value("globals/button_height").toInt());
    bt->setFocusPolicy(Qt::NoFocus);
    connect(bt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(bt, sName);
    layout->addWidget(bt,4,3);

    sName = "<-";
    bt = new QPushButton(sName, this);
    bt->setMaximumSize(settings->value("globals/button_width").toInt(), settings->value("globals/button_height").toInt());
    bt->setFocusPolicy(Qt::NoFocus);
    connect(bt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(bt, sName);
    layout->addWidget(bt,0,3);

    QHBoxLayout *leLayout = new QHBoxLayout();
    leLayout->setContentsMargins(0,0,0,0);
    leLayout->setMargin(0);
    leLayout->setSpacing(0);

    lblMode = new QLabel(this);
    leLayout->addWidget(lblMode);

    le = new QLineEdit(this);
    le->setMaximumHeight(settings->value("globals/button_height").toInt());
    le->setMaxLength(20);
    connect(le, SIGNAL(textChanged(QString)), this, SIGNAL(inputChanged(QString)) );

    leLayout->addWidget(le);
    //layout->addWidget(le,0,0,1,3);

    layout->addLayout(leLayout, 0, 0, 1, 3);

    // must be CHANGED to corrent detect position
    lblMode->setText("-");
    lblMode->setText("");
    //connect(lblMode, SIGNAL(, this, SIGNAL(inputChanged(QString)) );

    connect(signalMapper, SIGNAL(mapped(QString)),
            this, SLOT(actButtonClicked(QString)));

    setFocusProxy(le);
    this->installEventFilter(this);
}

void Numpad::setEchoMode(QLineEdit::EchoMode mode){
        le->setEchoMode(mode);
}

void Numpad::setModeLabel(const QString &str){
    lblMode->setText(str);
}



Numpad::~Numpad()
{
}

void Numpad::actButtonClicked(const QString &buttontext){
    bool isExclusion = false;

    if (buttontext=="<-"){
        le->setText(le->text().left(le->text().length()-1));
        isExclusion = true;
    }
    if (buttontext=="Ok"){
        emit inputCommited(le->text());
        le->setText("");
        isExclusion = true;
    }
    if(!isExclusion)
        le->setText(le->text()+buttontext);

}


QString Numpad::getText(){
    return le->text();
}

void Numpad::clearText(){
    le->setText("");
}

/*
void Numpad::modeLabelClicked(const QString &text){


}
*/

bool Numpad::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

        if(keyEvent->key()==Qt::Key_Enter || keyEvent->key()==Qt::Key_Return){
            actButtonClicked("Ok");
            return true;
        }
    }

    return QWidget::eventFilter(obj, event);
}

